package com.ld.admin.popup.tables;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ld.admin.controller.AdminBasicController;
import com.ld.admin.service.codegen.CodegenService;
import com.ld.shieldsb.dao.model.PageNavigationBean;
import com.ld.shieldsb.db.model.DBSetInfo;
import com.ld.shieldsb.db.model.DBTableModel;
import com.ld.shieldsb.db.service.DBTabelService;

import lombok.extern.slf4j.Slf4j;

/**
 * 选择器
 * 
 * @ClassName UserPopupController
 * @author <a href="mailto:550827627@qq.com" target="_blank">刘金浩</a>
 * @date 2019年7月15日 上午9:03:17
 *
 */
@Slf4j
@Controller
@RequestMapping("${shieldsb.adminPath}/popup/dbbackup/tables")
public class DbTablesPopupController extends AdminBasicController<DBTableModel> {

    @Autowired
    public CodegenService codegenService;

    /**
     * 
     * TODO( 选择器搜索页面)
     * 
     * @Title toSearch
     * @author 刘金浩
     * @date 2019年7月15日 上午9:08:17
     * @return String
     */
    @RequestMapping("toSearch")
    public String toSearch() {
        setAttributes();
        return "a/system/dbbackup/popup/tables/tablesPopUp_search";
    }

    /**
     * 
     * TOD( 选择器列表页面)
     * 
     * @Title search
     * @author 刘金浩
     * @date 2019年7月15日 上午9:08:49
     * @return String
     */
    @RequestMapping("search")
    public String search() {
        setAttributes();
        int pageNum = getInt("p");
        int pageSize = 10;
        Long dbConnId = getLong("dbConnId");
        DBSetInfo dbSetInfo = codegenService.getDbSetInfo(dbConnId);
        DBTabelService service = DBTabelService.getInstance(dbSetInfo);
        String tableName = getString("tableName");
        List<DBTableModel> dbList = service.getTables(tableName);
        PageNavigationBean<DBTableModel> pageBean = getPageNavigationBean(dbList, pageNum, pageSize);
        request.setAttribute("pageBean", pageBean); // list
        return "a/system/dbbackup/popup/tables/tablesPopUp_list";
    }

    /**
     * 
     * 分页
     * 
     * @Title getPageNavigationBean
     * @author 刘金浩
     * @date 2019年11月19日 上午11:35:14
     * @param list
     * @param pageNumber
     * @param pageSize
     * @return PageNavigationBean<DBTableModel>
     */
    public static PageNavigationBean<DBTableModel> getPageNavigationBean(List<DBTableModel> list, int pageNumber, int pageSize) {
        List<DBTableModel> pageList = new ArrayList<>();
        int currIdx = (pageNumber > 1 ? (pageNumber - 1) * pageSize : 0);
        for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) {
            DBTableModel listNew = list.get(currIdx + i);
            pageList.add(listNew);
        }
        PageNavigationBean<DBTableModel> pageBean = new PageNavigationBean<>();
        pageBean.setResultList(pageList);
        pageBean.setCurrentPage(pageNumber);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(list.size());

        return pageBean;
    }
}
